package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuStatusService;
import com.itheima.health.service.RoleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("systemsetting")
@RestController
public class SystemController {
    @DubboReference
    private RoleService roleService;
    @DubboReference
    private MenuStatusService menuStatusService;

    @GetMapping("getmenulist")
    public Result getmenulist() {
        //获取当前用户名
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;
        if (null != obj && obj instanceof User) {
            //注意此处的user是security包下面的，不是我们的pojo下面的实体
            User user = (User) obj;
            username = user.getUsername();
        }
        List<Menu> menuList = menuStatusService.getmenulist(username);
        return new Result(true, MessageConst.ACTION_SUCCESS, menuList);
    }
}
